package dao.Impl;

import Bean.Product;
import Dbutlis.Dbutli;
import dao.ProductDao;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * ClossName:ProductImpl
 * Pakage:dao.Impl
 * Description
 *
 * @Author L
 * @Create 2023/10/26 10:14
 * @Version 2023.1.3
 */
public class ProductImpl implements ProductDao {
    @Override
    public int inioProduct(Product product) {
        int rows = 0;
        //1)sql
        String sql = "insert into Prod values(?,?,?,?,?)";

        //2)获取连接
        Connection conn = Dbutli.getConnection();

        PreparedStatement prstm = null;
        try {
            //3)预编译
            prstm = conn.prepareStatement(sql);

            prstm.setInt(1,product.getProdId());
            prstm.setString(2, product.getProdName());
            prstm.setDouble(3,product.getProdPrice());
            prstm.setDate(4,(Date) product.getProdNow());
            prstm.setInt(5,product.getProdIsno());

            rows = prstm.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            Dbutli.close(conn,prstm,null);
        }
        return rows;
    }

    @Override
    public int deleteProduct(int id) {
        int rows = 0;

        //sql
        String sql = "delete Prod where prodId=?";
        //获取链接
        Connection conn = Dbutli.getConnection();

        PreparedStatement pste = null;
        try {
             //预编译
             pste = conn.prepareStatement(sql);
             //填充占位符
             pste.setInt(1,id);
             //执行sql
             rows = pste.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            Dbutli.close(conn,pste,null);
        }
        return rows;
    }

    @Override
    public int updateProduct(Product product) {

        int rows = 0;

        //sql
        String sql = "update set prodName=?,prodPrice=?,prodNow=?,prodIsno=? where prodId=?";

        //获取链接
        Connection conn = Dbutli.getConnection();
        PreparedStatement prst = null;
        //预编译
        try {
             prst = conn.prepareStatement(sql);

             prst.setString(2,product.getProdName());
             prst.setDouble(3,product.getProdPrice());
             prst.setDate(4,(Date) product.getProdNow());

             //执行sql
            rows = prst.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            Dbutli.close(conn,prst,null);
        }
        return rows;
    }

    @Override
    public List<Product> selectProduct() {

        //sql
        String sql = "select * from Prod";

        //获取连接
        Connection conn  = Dbutli.getConnection();

        PreparedStatement prstm = null;
        ResultSet rst = null;

        List<Product> list = new ArrayList<>();
        try {
             //预编译
             prstm = conn.prepareStatement(sql);

             //执行sql
             rst = prstm.executeQuery();

            //查询到数据执行
            while (rst.next()){
                Product product = new Product();
                product.setProdId(rst.getInt(1));
                product.setProdName(rst.getString(2));
                product.setProdPrice(rst.getDouble(3));
                product.setProdNow(rst.getDate(4));
                product.setProdIsno(rst.getInt(5));

                //添加到集合
                list.add(product);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            Dbutli.close(conn,prstm,rst);
        }
        return list;
    }
}
